//
//  PGHttpOperator.m
//  PhotographicGuide
//
//  Created by indream on 13-2-23.
//  Copyright (c) 2013年 indream. All rights reserved.
//

#import "PGHttpOperator.h"
#import "PGLocalFileManager.h"

@implementation PGHttpOperator
- (id)initWithURLString:(NSString *)url cache:(BOOL)cache notification:(NSString*)notification{
    self = [self init];
    if (self) {
        NSLog(@"%@",url);
        self.url = url;
        self.cache = cache;
        self.notification = notification;
    }
    
    return self;
    
}
-(BOOL)isConcurrent{
    return YES;
}
- (BOOL)isExecuting
{
    return _conn == nil;
}
- (BOOL)isFinished
{
    return _conn == nil;
}
-(void)start{
    if(self.cache){
        if([[PGLocalFileManager sharedManager]fileIsExist:self.url]){
            self.data = [NSMutableData dataWithData:[[PGLocalFileManager sharedManager]fileWithURL:self.url]];
            [[NSNotificationCenter defaultCenter]postNotificationName:self.notification object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:self.url,@"url",_data,@"data", nil]];
            self.conn = nil;
            return;
        }
    }
    if(_conn==nil){
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url]];
        if(_postString){
            NSData *postData = [NSData dataWithBytes: [_postString UTF8String] length: [_postString length]];
            [request setHTTPMethod: @"POST"];
            [request setHTTPBody: postData];
            [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
            [request setHTTPMethod: @"POST"];
            
        }
        self.conn = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
        while(_conn != nil) {
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
        }
    }
    
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    self.data = [NSMutableData data];
}
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response{
    return request;
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [self.data appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"connection did finish!");
    [[PGLocalFileManager sharedManager]saveFile:self.data withFileURL:self.url];
    [[NSNotificationCenter defaultCenter]postNotificationName:self.notification object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:self.url,@"url",_data,@"data", nil]];
    self.conn = nil;
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"connection did fail!");
    [[NSNotificationCenter defaultCenter]postNotificationName:self.notification object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:self.url,@"url",_data,@"data", nil]];
    self.conn = nil;
}
@end